<!--
 * @Author: gaoyu gy15354315043@163.com
 * @Date: 2024-10-25 10:30:19
 * @LastEditors: 郝奔 1425466221@qq.com
 * @LastEditTime: 2025-01-02 10:40:14
 * @Description: 应用中心列表
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved. 
-->
<template>
    <div class="applicationList">
        <div class="applicationHeader">
            <div class="title">应用中心</div>
            <el-icon @click="closeDrawer">
                <Close />
            </el-icon>
        </div>
        <div class="applicationSearch">
            <el-input v-model="applicationValue" size="large" placeholder="请输入内容" :prefix-icon="Search"
                :class="applicationValue ? 'activeInput' : 'inActiveInput'" />
        </div>
        <div class="applicationContent" v-loading="stateData.loading">
            <div class="recentlyVisited">
                <div class="contentTitle">最近访问</div>
                <el-row :gutter="16" class="recentlyContent">
                    <div v-if="stateData.list.length > 0" class="hasData">
                        <el-col :span="8" v-for="(item, index) in stateData.list" :key="index" class="contentItem"
                            @click="handlAppRoute(item)">
                            {{ item.appName }}
                        </el-col>
                    </div>
                    <div v-else class="contentItem empty">暂无</div>
                </el-row>
            </div>
            <div class="applicationClassification">
                <div v-for="(item, index) in filterApplicationClassify" :key="index" class="classifyContent">
                    <div class="contentTitle" v-if="item.appList.length > 0">{{ item.appTypeName }}</div>
                    <el-row :gutter="16" class="data">
                        <el-col :span="8" v-for="(app, index) in item.appList" :key="index" class="contentItem"
                            @click="handlAppRoute(app)">
                            <div v-dompurify-html="highlightText(app.appName, applicationValue)"></div>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { Search } from '@element-plus/icons-vue'
import { getRecentVisitAppList, getLoginUserApp, addRecentVisitApp } from '../../api/index'
import { appList, treeData } from '../../api/types'
// import { getToken } from '@/framework/utils/auth';
import { openOrPushUrl } from '../../../../../utils/index'
const closeGrid = inject('closeGrid');
const applicationValue = ref('')
const emits = defineEmits(['closeDrawer'])
interface dataType {
    list: appList[],
    applicationClassifyList: treeData[],
    loading:boolean
}
const stateData = reactive<dataType>({
    list: [],
    applicationClassifyList: [],
    loading: false
})
const props = defineProps({
    /**
    * @description: 父组件drawer是否可见
    * @return {*}
    */
    drawerVisible: {
        type: Boolean,
        default: false
    },
})
watch(
    () => props.drawerVisible,
    () => {
        applicationValue.value = ''
    }
);
onMounted(() => {
    initData()
})
const filterApplicationClassify = computed(() => {
    if (!applicationValue.value) {
        return stateData.applicationClassifyList
    }
    return (stateData.applicationClassifyList || []).map(classify => ({
        ...classify,
        appList: (classify.appList || []).filter(item => item.appName.includes(applicationValue.value))
    }));
})
/**
 * @description: 关闭弹窗
 * @return {*}
 */
function closeDrawer(): void {
    emits('closeDrawer');
}
/**
 * @description: 搜索应用
 * @return {*}
 */
function highlightText(text: string, query: string): string {
    if (!query) {
        return text;
    }
    const queryRegex = new RegExp(`(${query.replace(/[-/\\$*+?.()|[\]{}]/g, '\\$&')})`, 'gi');
    return text.replace(queryRegex, '<span class="highlight">$1</span>');
}
/**
 * @description: 初始化数据
 * @return {*}
 */
function initData(): void {
    getRecentVisitApp()
    getUserApp()
}
/**
 * @description: 获取当前登录用户最近访问的应用列表
 * @return {*}
 */
async function getRecentVisitApp(): Promise<void> {
    const res = await getRecentVisitAppList()
    stateData.list = res.data.map(item => {
        const defaultMenuPath = item.defaultMenuPath ? item.defaultMenuPath : ''
        return {
            ...item,
            link: item.appAddress + defaultMenuPath,
            // link: item.appAddress + (specialData.includes(item.appCode) ? '' : defaultMenuPath),
        }
    })
}
/**
 * @description: 获取当前登录用户有权限的应用列表
 * @return {*}
 */
async function getUserApp(): Promise<void> {
    const res = await getLoginUserApp()
    stateData.applicationClassifyList = res.data.map(item => {
        return {
            ...item,
            appList: item.appList.map(app => {
                const defaultMenuPath = app.defaultMenuPath ? app.defaultMenuPath : ''
                return {
                    ...app,
                    link: app.appAddress + defaultMenuPath,
                    // link: app.appAddress + (specialData.includes(app.appCode) ? '' : defaultMenuPath),
                }
            }),
        }
    })
}
/**
 * @description: 点击应用
 * @return {*}
 */
async function handlAppRoute(app: appList): Promise<void> {
    try{
        await addRecentVisitApp({ appCode: app.appCode })
        // openOrPushUrl(app, app.link)
        openOrPushUrl(app, app.link, closeGrid)
        // window.open(`${app.link}?token=${getToken()}`)
        stateData.loading = false
    } catch(error) {
        stateData.loading = false
    }
}
defineExpose({
    initData
})
</script>
<style scoped lang="scss">
.applicationList {
    box-sizing: border-box;
    padding: 16px 16px 0;

    .applicationHeader {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;

        .title {
            font-size: 18px;
            font-weight: bold;
            line-height: 26px;
        }
    }

    .applicationSearch {
        line-height: 40px;

        :deep(.el-icon) {
            font-size: 16px;
            margin-right: 5px;
        }



        :deep(.el-input__wrapper) {
            box-shadow: none;
            border-radius: 0;
            padding: 8px 12px;
        }

        .activeInput {
            border-bottom: 1px solid var(--primary-color) !important;
        }

        .inActiveInput {
            border-bottom: 1px solid var(--el-input-border-color) !important;
        }

        :deep(.el-input__inner) {
            line-height: 22px;
            height: 22px;
        }


    }

    .applicationContent {
        margin-top: 24px;
        overflow: auto;
        height: calc(100vh - 184px);

        :deep(.el-row) {
            margin-right: 0 !important;
            margin-left: 0 !important;
        }

        .recentlyContent {
            margin-bottom: 32px;

            .hasData {
                width: 100%;
                display: flex;
                flex-wrap: wrap;
            }
        }

        .contentTitle {
            height: 22px;
            margin: 0 8px;
            font-size: 14px;
            font-weight: 700;
            color: #000000;
            line-height: 22px;
            margin-bottom: 8px;
        }

        .contentItem {
            box-sizing: border-box;
            padding: 5px 8px;
            font-size: 14px;
            line-height: 26px;
            color: #131414;
            cursor: pointer;
        }

        .empty {
            padding: 7px 8px;
            line-height: 22px;
            color: #606266;
        }

        .classifyContent {
            margin-bottom: 32px;

            :deep(.highlight) {
                color: var(--primary-color);

            }

            .classifyContent:last-child {
                margin-bottom: 60px;
            }

        }
    }

}
</style>